<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="24">
            <el-form-item label="图片" prop="image"
                :rules="[
                 ]">
              <el-upload ref="image"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/eventofticket/eventWeixinInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.image = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.image = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="imageArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标题" prop="title"
                :rules="[
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开始日期" prop="begintime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.begintime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="结束日期" prop="endtime"
                :rules="[
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.endtime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="省市县" prop="address"
                :rules="[
                 ]">
                      <CityPicker 
                         style="width:100%"
                        :value="inputForm.address"
                        :clearable="true" 
                        :accordion="true"
                        @getValue="(value) => {inputForm.address=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="详细地址" prop="detailaddress"
                :rules="[
                 ]">
              <el-input v-model="inputForm.detailaddress" placeholder="请填写详细地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="大会背景" prop="eventdetail"
                :rules="[
                 ]">
              <el-input v-model="inputForm.eventdetail" placeholder="请填写大会背景"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="活动签到二维码图片" prop="enteredcode"
                :rules="[
                 ]">
              <el-upload ref="enteredcode"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/eventofticket/eventWeixinInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.enteredcode = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.enteredcode = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="enteredcodeArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="活动二维码图片" prop="eventcode"
                :rules="[
                 ]">
              <el-upload ref="eventcode"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/eventofticket/eventWeixinInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.eventcode = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.eventcode = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="eventcodeArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="技术支持" prop="technicalsupport"
                :rules="[
                 ]">
              <el-upload ref="technicalsupport"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/eventofticket/eventWeixinInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file) => {$window.open ((file.response && file.response.url) || file.url,'_blank') }"
                    :on-success="(response, file, fileList) => {
                       inputForm.technicalsupport = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.technicalsupport = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="technicalsupportArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="参会对象" prop="participants"
                :rules="[
                 ]">
              <el-input v-model="inputForm.participants" placeholder="请填写参会对象"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="费用" prop="cost.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择费用"
            labelName = 'ticketname'
            labelValue = 'id'
            :value = "inputForm.cost.id"
            :limit="1"
            @getValue='(value) => {inputForm.cost.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'ticketname',
              label: '票种名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'ticketname',
              label: '票种名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/ticketsevent/eventTicket/list"
            entityBeanName="eventTicket"
            queryEntityUrl="/dlyrl/hr/ticketsevent/eventTicket/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="主办方" prop="organizer"
                :rules="[
                 ]">
              <el-input v-model="inputForm.organizer" placeholder="请填写主办方"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="咨询电话" prop="consultationphone"
                :rules="[
                 ]">
              <el-input v-model="inputForm.consultationphone" placeholder="请填写咨询电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签到范围" prop="signscope"
                :rules="[
                 ]">
              <el-input v-model="inputForm.signscope" placeholder="请填写签到范围"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import CityPicker from '@/components/cityPicker'
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        imageArra: [],
        enteredcodeArra: [],
        eventcodeArra: [],
        technicalsupportArra: [],
        inputForm: {
          id: '',
          image: '',
          title: '',
          begintime: '',
          endtime: '',
          address: '',
          detailaddress: '',
          eventdetail: '',
          enteredcode: '',
          eventcode: '',
          technicalsupport: '',
          participants: '',
          cost: {
            id: ''
          },
          organizer: '',
          consultationphone: '',
          signscope: '',
          remarks: ''
        }
      }
    },
    components: {
      CityPicker,
      GridSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建活动表`
        } else if (method === 'edit') {
          this.title = '修改活动表'
        } else if (method === 'view') {
          this.title = '查看活动表'
        }
        this.imageArra = []
        this.enteredcodeArra = []
        this.eventcodeArra = []
        this.technicalsupportArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/eventofticket/eventWeixinInformation/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.eventWeixinInformation)
              this.inputForm.image.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.imageArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.enteredcode.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.enteredcodeArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.eventcode.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.eventcodeArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.technicalsupport.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.technicalsupportArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/eventofticket/eventWeixinInformation/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>